package com.liu.communitysystemend.module.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.user.domain.User;
import com.liu.communitysystemend.module.user.domain.UserDto;
import com.liu.communitysystemend.module.user.mapper.UserMapper;
import com.liu.communitysystemend.module.user.service.UserService;
import com.liu.communitysystemend.utils.RedisUtil;
import com.liu.communitysystemend.utils.Result;
import com.liu.communitysystemend.utils.WStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
*
*/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public User getUserById(String id) {
        if (id==null||id=="" ){
            throw new CommonException(501,"id参数为空");
        }
        User user = userMapper.selectById(id);
        return user;

    }

    @Override
    public User update(User user) {
        if (!"".equals(user.getPassword()) && user.getPassword() != null) {
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        } else {
            user.setPassword(null);
        }
        int i = userMapper.updateById(user);
        if (i<0){
            throw new SavaException("更改用户失败");
        }
        return user;

    }



    @Override
    public Result register(Map<String, String> registerUser) {
        User user = new User();
        user.setUsername(registerUser.get("username"));
        user.setPassword(bCryptPasswordEncoder.encode(registerUser.get("password")));
        String role=null;
        switch (registerUser.get("role")){
            case "0":
                role = "admin";
                user.setStatus(1);
                break;
            case "1":
                role = "user";
                user.setStatus(1);
                break;
            default:
                return Result.failed("角色异常");
        }
        user.setRole(role);
        userMapper.insert(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("user",user);
        return Result.success(map);
    }

    public  User selectByUserName(User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        wrapper.eq(User::getIsDeleted,0);
        User user1 = userMapper.selectOne(wrapper);
        return user1;
    }

    @Override
    public User add(User userDto) {
        User user = new User();
        User user1 = selectByUserName(userDto);
        if (user1!=null){
            throw new CommonException("该用户名已存在");
        }
        BeanUtils.copyProperties(userDto,user);
        user.setUsername(userDto.getUsername());
        user.setRole("user");
        user.setStatus(1);
        user.setPassword(bCryptPasswordEncoder.encode("123456"));
        int insert = userMapper.insert(user);
        if (insert>0){
            return user;
        }
        throw new CommonException("添加失败");
    }

    @Override
    public Page<User> getPage(User user) {
        LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(User::getIsDeleted,0);
        if (!WStringUtils.isBlank(user.getNickname())){
            wrapper.like(User::getNickname,user.getNickname());
        }
        if (!WStringUtils.isBlank(user.getUsername())){
            wrapper.like(User::getUsername,user.getUsername());
        }
        if (!WStringUtils.isBlank(user.getIdCard())){
            wrapper.like(User::getIdCard,user.getIdCard());
        }
        Page<User> page = new Page<>(user.getPage().getPage(), user.getPage().getSize());
        Page<User> userIPage = (Page<User>)userMapper.selectPage(page, wrapper);
        return userIPage;
    }

    public User selectById(String id){
        User user = userMapper.selectById(id);
        return user;

    }



    @Override
    public Integer deleteById(String id) {
        User user = selectById(id);
        user.setIsDeleted(1);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,user.getId());
        int update = userMapper.update(user, wrapper);
        return update;
    }

    @Override
    public User selectOneByIdReturn(String uid) {
        User data  = getUserById(uid);
        return data;

    }

    @Override
    public User selectByIdCard(String idCard) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getIdCard,idCard);
        wrapper.eq(User::getStatus,1);
        wrapper.eq(User::getIsDeleted,0);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            User user = new User();
            user.setId("100"+i);
            user.setRoomId("100"+i);
            user.setRole("user");
            user.setStatus(1);
            user.setCarNo("100"+i);
            user.setIdCard("4232312312121123"+i);
            user.setPhone("15219023221"+i);
            user.setHeads("a9219fe9-6454-43c9-9fec-3fb6a7cc304d.jpg");
            user.setUsername("test100"+i);
            user.setNickname("业主100"+i);
            user.setPassword(bCryptPasswordEncoder.encode("123456"));
            userMapper.insert(user);
        }
        return null;
    }

    @Override
    public User selectByRoomId(String roomId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getRoomId,roomId);
        wrapper.eq(User::getStatus,1);
        wrapper.eq(User::getIsDeleted,0);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public User selectByCarId(String carno) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getCarNo,carno);
        wrapper.eq(User::getStatus,1);
        wrapper.eq(User::getIsDeleted,0);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    public Page<UserDto> getAllPage(User user){
        Page<UserDto> userDtoPage = new Page<>(user.getPage().getPage(),user.getPage().getSize());
        return userMapper.getAllPage(userDtoPage,user);
    }
}
